/*
 * 娜卡系统<conflux 区块链社区>
 */
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SimpleJson;
using System.Linq;

namespace Dolls
{
    /// <summary>
    /// 娜卡主要类型
    /// </summary>
    public enum eDollsType
    {
        Null,
        FIGHTER, //战斗类型
        ASSIST,  //辅助类型
    }

    /// <summary>
    /// 娜卡辅助子类型
    /// </summary>
    public enum eDollsAssistType
    {
        Null,
        PRODUCT,   //生产型
        BUILDING,  //建造型
        RESEARCH,  //研究型
        BASE,      //基地
    }

    /// <summary>
    /// 娜卡数据类
    /// </summary>
    public class cDoll
    {
        public CDollsResData mResData;          //资源数据

        public int mLevel;
        public int mExp;                //经验值
        public float mAtkAdd;
        public float mDefAdd;
        public float mDodgeAdd;
        public int mLove;
        public float mProductAdd;       //
        public float mCollectAdd;       //
        public float mResearchAdd;      //

        public float mAtkAddNew;
        public float mDefAddNew;
        public float mDodgeAddNew;
        public float mProductAddNew;       //
        public float mCollectAddNew;       //
        public float mResearchAddNew;      //
        //是否孵化中
        //0 尚未开始孵化 1孵化中 2 孵化完成
        public int mHatchingIndex;

        /// <summary>
        /// 获得当前好感度
        /// </summary>
        /// <returns></returns>
        public int GetLove()
        {
            return mLove > 20 ? mLove : 20;
        }

        public string Name()
        {
            return mResData.nameE;
        }

        public void ClearNewAdd()
        {
            mAtkAddNew = 0;
            mDefAddNew = 0;
            mDodgeAddNew = 0;
            mProductAddNew = 0;       //
            mCollectAddNew = 0;       //
            mResearchAddNew = 0;      //
        }

        public void Save()
        {
            JsonObject data = new JsonObject();
            data.Add("mLevel", mLevel);
            data.Add("mExp", mExp);
            data.Add("mAtkAdd", mAtkAdd);
            data.Add("mDefAdd", mDefAdd);
            data.Add("mDodgeAdd", mDodgeAdd);
            data.Add("mLove", mLove);
            data.Add("mProductAdd", mProductAdd);
            data.Add("mCollectAdd", mCollectAdd);
            data.Add("mResearchAdd", mResearchAdd);
            data.Add("mHatchingIndex", mHatchingIndex);
            string str = data.ToString();

            PlayerPrefs.SetString("dolls_" + mResData.mResId, str);
            PlayerPrefs.Save();
        }

        public void Load()
        {
            string str = PlayerPrefs.GetString("dolls_" + mResData.mResId, "");
            if (!string.IsNullOrEmpty(str))
            {
                JsonObject o = (JsonObject)SimpleJson.SimpleJson.DeserializeObject(str);

                mLevel = Convert.ToInt32(o["mLevel"]);
                mExp = Convert.ToInt32(o["mExp"]);
                mAtkAdd = Convert.ToSingle(o["mAtkAdd"]);
                mDefAdd = Convert.ToSingle(o["mDefAdd"]);
                mDodgeAdd = Convert.ToSingle(o["mDodgeAdd"]);
                mLove = Convert.ToInt32(o["mLove"]);
                mProductAdd = Convert.ToSingle(o["mProductAdd"]);
                mCollectAdd = Convert.ToSingle(o["mCollectAdd"]);
                mResearchAdd = Convert.ToSingle(o["mResearchAdd"]);
                mHatchingIndex = Convert.ToInt32(o["mHatchingIndex"]);
            }
            else
            {
                ReInit();
            }

        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void ReInit()
        {
            mLevel = 0;
            mExp = 0;
            mAtkAdd =
            mDefAdd =
            mDodgeAdd = 0;
            mLove = 0;
            mProductAdd =
            mCollectAdd =
            mResearchAdd =
            mHatchingIndex = 0;
        }
    }

    /// <summary>
    /// 娜卡资源数据
    /// </summary>
    public class CDollsResData
    {
        public long mResId; //资源类型
        public string name;
        public string nameE;
        public string des;
        public string desE;
        public eDollsType mainType;   //主类型
        public eMachineType warSubType; //战斗子类型
        public eDollsAssistType assistSubType; //辅助自类型
        public string iconPath;
        public string bigIconPath;
    }


    /// <summary>
    /// 所有的娜卡管理类
    /// </summary>
    public class CDollsManager : cDataBase<CDollsResData, CDollsManager>
    {
        public List<cDoll> mDolls = new List<cDoll>();

        public cDoll Find(long Id)
        {
            return mDolls.Find(c => c.mResData.mResId == Id);
        }

        protected override string GetPath()
        {
            return "Assets/project/res/NaKa.json";
        }

        protected override void ReadData(JsonArray array)
        {
            foreach (JsonObject item in array.Cast<JsonObject>())
            {
                CDollsResData info = new()
                {
                    mResId = Convert.ToInt32(item["index"]),
                    name = Convert.ToString(item["name"]),
                    nameE = Convert.ToString(item["nameE"]),
                    des = Convert.ToString(item["des"]),
                    desE = Convert.ToString(item["desE"]),
                    mainType = Enum.Parse<eDollsType>(Convert.ToString(item["type"])),
                    warSubType = Enum.Parse<eMachineType>(Convert.ToString(item["atkType"])),
                    assistSubType = Enum.Parse<eDollsAssistType>(Convert.ToString(item["assistType"])),
                    iconPath = Convert.ToString(item["icon"]),
                    bigIconPath = Convert.ToString(item["bigIcon"]),

                };
                mDict.Add(info.mResId, info);
            }

            gameUtility.Log("read dolls.json ok");

            LoadDolls();
        }

        public void LoadDolls()
        {
            mDolls.Clear();

            foreach (var v in mDict)
            {
                cDoll doll = new();
                doll.mResData = v.Value;
                doll.Load();
                mDolls.Add(doll);
            }
        }

        public void Save()
        {
            foreach (var v in mDolls)
            {
                v.Save();
            }
        }


        public  cDoll GetRandomNoHatchOne()
        {
            cDoll[] arr = mDolls.FindAll(c => c.mHatchingIndex == 0).ToArray();
            if(arr.Length>0)
            {
                int index = UnityEngine.Random.Range(0, arr.Length);
                return arr[index];
            }
            return null;
        }

    }



}

